import ajax from 'axios'
import sa from './util'
import jsonp from 'jsonp'

let host = '',
    // host = 'http://q.gdyunlifang.com',
    loginUrl = host + '/login',
    logoutUrl = host + '/logout',
    queryLeastUrl = host + '/data/latest/',
    queryLeastAllUrl = host + '/history/latest/',
    changePushUrl = host +'/3rd/';

ajax.defaults.withCredentials = true;

// ajax.defaults.headers['withcredentials'] = true;
//处理请求头
//同时接收一个提示回调
function disposeJsonHeader(json, tip) {
  var result = false;

  if (json && json.code === 0) {
    if (json.data) {
      if (!Object.keys(json.data).length) {
        if (error) {
          error();
        } else {
          if(tip) tip(json.msg);
        }
      } else {
        result = json.data;
      }
    } else {
      result = true;
    }

  } else if (json.code == 401) {
    if(tip) tip('登录信息失效，请重新登录');
    saLogout();

  } else {
    if(tip) tip(json.msg);
  }

  return result;
}

//普通的get请求
function normalGet(url, params, success, error, tip, always) {
  ajax.get(url, {params: params,withCredentials:true}).then((res) => {
    var data = disposeJsonHeader(res.data, tip);
    console.log(data);
    if (data) {
      try {
        if(success) success(data);
      } catch (e) {
        console.warn(e);
      }

    } else {
      if(error) error();
    }

    if (always) always();
  }).catch((err) => {
    // tip('获取数据失败');
    if (error) error();
    if (always) always();
  })
}

//普通post请求
function normalPost(url, params, success, error, tip, always) {
  ajax.post(url, params).then((res) => {
    let data = disposeJsonHeader(res.data, tip);
    if (data) {
      try {
        if(success) success(data);
      } catch (e) {
        console.warn(e);
      }
    } else {

    }
    if (always) always();

  }).catch((err) => {
    if (error) error();
    if (always) always();
  })
}

//设置token
function saSetToken(t){
  let token = '';

  if(t) {
    token = t;
    sa.localSave('clientRadarToken',token);
  }else{
    token = sa.localSave('clientRadarToken');
  }

  ajax.defaults.headers.token = token;
}


//登录
function saLogin(username, password, success, error, tip,always) {
  let url = loginUrl,
    data = {
      username: username,
      password: password
    };

  let suc = (res) => {
      if (res.data.code == 0) {
        success();
      } else {
        tip(res.data.msg || '登录失败，账号或密码错误');
      }

      if(always) always();
    },
    err = (err) => {
      tip.log('登录失败，请检查网络...');
      if(always) always();
    }

  ajax.post(url, data).then(suc).catch(err);
}

function saLogout() {
  let url = logoutUrl;
  console.log('logout');
  normalGet(url);
  location.href = '#/login';
}

function saChengePush(macs,success,error,tip,always){
  let url = changePushUrl + macs.join('/');
  console.log(url);
  normalGet(url,'',success,error,tip,always);
}

function saQueryLeast(params, success, error, tip, always) {
  let url = queryLeastUrl + params.join('/');
  normalGet(url, '', success, error, tip, always);
  // jsonp(url,(res) =>{
  //   console.log(res);
  //   success(res);
  // },(err) =>{
  //   error();
  // })
}


function saQeuryLeastAll(params, success, error, tip, always) {
  let url = queryLeastAllUrl + params.join('/');
  normalGet(url, '', success, error, tip, always);
  // jsonp(url,(res) =>{
  //   console.log(res);
  //   success(res);
  // },(err) =>{
  //   error();
  // })
}

let methods = {
  sa,
  saLogin,
  saLogout,
  saChengePush,
  saQueryLeast,
  saQeuryLeastAll,
}

export default {
  install: function (Vue, option) {
    for (let k in methods)
      Vue.prototype[k] = methods[k]
  }
}
